-- @Author: baidwwy
-- @Date:   2018-04-10 22:52:12
-- @Last Modified by:   baidwwy
-- @Last Modified time: 2018-08-11 16:22:56

local GGELUA = class()

local _eq       = function (a,b) return a.x==b.x and a.y==b.y end-- (==)
local _lt       = function (a,b) return a.x<b.x and a.y<b.y end-- (<)
local _le       = function (a,b) return a.x<=b.x and a.y<=b.y end-- (<=)
local _add      = function (a,b)-- (+)
    if type(a)=='table' and type(b)=='table' then
        a.x = a.x or a[1];a.y = a.y or a[2]
        b.x = b.x or b[1];b.y = b.y or b[2]
        return GGELUA(a.x + b.x,a.y + b.y)
    elseif type(a) =='number'then
        return GGELUA(a + b.x,a + b.y)
    elseif type(b) =='number'then
        return GGELUA(a.x + b,a.y + b)
    end
end
local _sub      = function (a,b)-- (-)
    if type(a)=='table' and type(b)=='table' then
        a.x = a.x or a[1];a.y = a.y or a[2]
        b.x = b.x or b[1];b.y = b.y or b[2]
        return GGELUA(a.x - b.x,a.y - b.y)
    elseif type(a) =='number'then
        return GGELUA(a - b.x,a - b.y)
    elseif type(b) =='number'then
        return GGELUA(a.x - b,a.y - b)
    end
end

local _mul      = function (a,b)-- (*)
    if type(a)=='table' and type(b)=='table' then
        a.x = a.x or a[1];a.y = a.y or a[2]
        b.x = b.x or b[1];b.y = b.y or b[2]
        return GGELUA(a.x * b.x,a.y * b.y)
    elseif type(a) =='number'then
        return GGELUA(a * b.x,a * b.y)
    elseif type(b) =='number'then
        return GGELUA(a.x * b,a.y * b)
    end
end

local _div      = function (a,b)-- (/)
    if type(a)=='table' and type(b)=='table' then
        a.x = a.x or a[1];a.y = a.y or a[2]
        b.x = b.x or b[1];b.y = b.y or b[2]
        return GGELUA(a.x / b.x,a.y / b.y)
    elseif type(a) =='number'then
        return GGELUA(a / b.x,a / b.y)
    elseif type(b) =='number'then
        return GGELUA(a.x / b,a.y / b)
    end
end

local _tostring = function (a,b) return string.format("%d\t%d",a.x,a.y) end
function GGELUA:初始化(x,y)
    self.x = tonumber(x) or 0
    self.y = tonumber(y) or 0

    local mt = getmetatable(self)
        mt.__eq       = _eq
        mt.__lt       = _lt
        mt.__le       = _le
        mt.__add      = _add
        mt.__sub      = _sub
        mt.__mul      = _mul
        mt.__div      = _div
        mt.__tostring = _tostring
end
function GGELUA:ceil()
    self.x = math.ceil(self.x)
    self.y = math.ceil(self.y)
    return self
end
function GGELUA:floor()
    self.x = math.floor(self.x)
    self.y = math.floor(self.y)
    return self
end
function GGELUA:unpack()
    return self.x,self.y
end
function GGELUA:更新(x,y)
    self.x = y and x or x.x or x[1]
    self.y = y and y or x.y or x[2]
    return self
end
function GGELUA:复制()
    return GGELUA(self.x,self.y)
end
function GGELUA:随机(x,x1,y,y1)
    self.x = math.random(x, x1)
    self.y = math.random(y, y1)
    return self
end
--取两点距离
function GGELUA:取距离(x,y)
    if type(x) == 'table' then
        x,y=x.x or 0,x.y or 0
    end
    return math.sqrt(math.pow(self.x-x,2) + math.pow(self.y-y,2))
end
--取两点弧度
function GGELUA:取弧度(_x,_y)
    local x,y = self.x,self.y
    if type(_x) == 'table' then
        _x,_y=_x.x or 0,_x.y or 0
    end
    if(_y ==y and _x==x)then
        return 0
    elseif(_y >=y and _x<=x)then
        return math.pi-math.abs(math.atan((_y-y)/(_x-x)))
    elseif(_y <=y and _x>=x)then
        return math.pi2-math.abs(math.atan((_y-y)/(_x-x)))
    elseif(_y <=y and _x<=x)then
        return math.atan((_y-y)/(_x-x))+math.pi
    elseif(_y >=y and _x>=x)then
        return math.atan((_y-y)/(_x-x))
    end
end
function GGELUA:取角度(x,y)--math.rad
    return math.deg(self:取弧度(x,y))
end
function GGELUA:取地图位置(w,h)--图块宽高
    return GGELUA(math.ceil(self.x/w), math.ceil(self.y/h))
end
function GGELUA:取距离坐标(r,a) --距离,弧度
    local x,y = 0,0
    x=r* math.cos(a)+self.x
    y=r* math.sin(a)+self.y
    return GGELUA(x,y)
end
function GGELUA:取移动坐标(r,x,y)--距离,目标点
    local a = self:取弧度 (x,y)
    return self:取距离坐标(r,a)
end
function GGELUA:移动(r,x,y)--距离,目标点
    local a = self:取弧度 (x,y)
    self.x = r* math.cos(a)+self.x
    self.y = r* math.sin(a)+self.y
    return self
end

return GGELUA